package com.erp.app.sendMail.web;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.erp.app.common.ProjectConstants;
import com.erp.app.common.select.beans.SelectDelegate;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.web.SimpleAction;

public class SendEmailAction extends SimpleAction{
    public static final String STATUS_SEND_EMAIL = "statusSendEmail";
    private String SubStr;

    protected ActionForward handlePageEvent(ActionMapping mapping, ActionForm formIn, HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {
         HttpSession session = request.getSession(false);
         SendEmailForm form = (SendEmailForm) formIn;
         String action = form.getAction();
         action = (action == null) ? "" : action.trim();
         ActionForward actionForward = new ActionForward(mapping.getInput());
         boolean handled = false;

         if(!CommonUtil.isEmpty((HashMap) session.getAttribute(ProjectConstants.SESSION_PARAMA_SEND_EMAIL))){
             HashMap params = (HashMap) session.getAttribute(ProjectConstants.SESSION_PARAMA_SEND_EMAIL);
             form.resetForm();
             session.removeAttribute(ProjectConstants.SESSION_PARAMA_SEND_EMAIL);
             if(ProjectConstants.GENERIC_YES.equals((String)params.get(ProjectConstants.SESSION_PARAM_SEND_EMAIL_SHOW_CC))){
                 form.setShowCC(ProjectConstants.GENERIC_YES);
             }else{
                 form.setShowCC(ProjectConstants.GENERIC_NO);
             }
             if(ProjectConstants.GENERIC_YES.equals((String)params.get(ProjectConstants.SESSION_PARAM_SEND_EMAIL_SHOW_BCC))){
                 form.setShowBCC(ProjectConstants.GENERIC_YES);
             }else{
                 form.setShowBCC(ProjectConstants.GENERIC_NO);
             }
             form.setParams(params);
             form.setFrom((String)params.get(ProjectConstants.SESSION_PARAM_SEND_EMAIL_FROM));
             //form.setSubject((String)params.get(ProjectConstants.PARAM_SEND_EMAIL_WITH_SUBJECT));

             String languageId = (String) session.getAttribute(ProjectConstants.SESSION_LANGUAGE_ID);
             String enter = "\nb";
             
             String fromStoryboard = (String) session.getAttribute(ProjectConstants.SESSION_STORYBOARD_EMAIL);
             System.out.println("SendemailAction->" + fromStoryboard);
             session.removeAttribute(ProjectConstants.SESSION_STORYBOARD_EMAIL);
             
             String subjectQuery="select PRJ_PARAM_VAL from T_PROJECT_PARAM where PRJ_PARAM_ID='" + ProjectConstants.PP_PRODUCT_CATALOG_SEND_EMAIL_SUBJECT + "'";
	 		 String messageQuery="select PRJ_PARAM_VAL from T_PROJECT_PARAM where PRJ_PARAM_ID='" + ProjectConstants.PP_PRODUCT_CATALOG_SEND_EMAIL_MESSAGE + "'";
	 		 SelectDelegate dg= new SelectDelegate();
        	 String subject = CommonUtil.nullToString(dg.selectOneValue(subjectQuery));
        	 String body = CommonUtil.nullToString(dg.selectOneValue(messageQuery));

             if(!CommonUtil.isEmpty(fromStoryboard) && fromStoryboard.equalsIgnoreCase(ProjectConstants.GENERIC_YES)){
 
                 String emailBody1 = (String) params.get(ProjectConstants.PARAM_SEND_EMAIL_BODY_1);
                 subject = CommonUtil.replace(subject, "%3", emailBody1);
                 StringBuffer bodybf = new StringBuffer(CommonUtil.replace(body, "%3", emailBody1+ "\n\n") );
                 form.setSubject(subject);
                 form.setBody(bodybf.toString());
                 form.setHideTxtDefaultBody(emailBody1);
                 String webLink = "<a href=\""+emailBody1 + "\" target='_blank' >" + emailBody1 + "</a>";
                 form.setHideHtmlDefaultBody(webLink);           	                  
             }else{                 
            	 String emailBody1 = (String) params.get(ProjectConstants.PARAM_SEND_EMAIL_BODY_1);
                 String emailBody2 = (String) params.get(ProjectConstants.PARAM_SEND_EMAIL_BODY_2);
                                        
                 subject = CommonUtil.replace(subject, "%1", emailBody1);
                 subject = CommonUtil.replace(subject, "%2", emailBody2);
                    //Will enhance this later
                 StringBuffer bodybf = new StringBuffer(CommonUtil.replace(body, "%1", emailBody1+ "\n\n") );
                 StringBuffer bodybf2 = new StringBuffer(CommonUtil.replace(bodybf.toString(), "%2", emailBody2));               
                 form.setSubject(subject);
                 form.setBody(bodybf2.toString());    
                 form.setHideTxtDefaultBody(emailBody2);
                    
                 String webLink = "<a href=\""+emailBody2 + "\" target='_blank' >" + emailBody2 + "</a>";               
                 form.setHideHtmlDefaultBody(webLink);
             }

         }

         if (!handled && action.equalsIgnoreCase("sendEmail1")) {
             HashMap params2 = form.getParams();
             String sessionValueName = (String) params2.get(ProjectConstants.PARAM_SEND_EMAIL_WITH_VALUE);
             if(!CommonUtil.isEmpty(form.getTo())){
                 String[] toAddress = form.getTo().split(",");
                 params2.put(ProjectConstants.PARAM_SEND_EMAIL_WITH_TO, toAddress);
             }
             if(!CommonUtil.isEmpty(form.getCc())){
                 String[] ccAddress = form.getTo().split(",");
                 params2.put(ProjectConstants.PARAM_SEND_EMAIL_WITH_CC, ccAddress);
             }
             if(!CommonUtil.isEmpty(form.getBcc())){
                 String[] bccAddress = form.getTo().split(",");
                 params2.put(ProjectConstants.PARAM_SEND_EMAIL_WITH_BCC, bccAddress);
             }
             if(!CommonUtil.isEmpty(form.getSubject())){
                 params2.put(ProjectConstants.PARAM_SEND_EMAIL_WITH_SUBJECT, form.getSubject());
             }
             if(!CommonUtil.isEmpty(form.getBody())){
            	 String bodyHtml = "";
            	 
            	 if(!CommonUtil.isEmpty(form.getHideHtmlDefaultBody()) && !CommonUtil.isEmpty(form.getHideTxtDefaultBody())){	
            		 bodyHtml = CommonUtil.replace(form.getBody(), "\r\n", "<br>");
            		 bodyHtml = CommonUtil.replace(bodyHtml, " ", "&nbsp");
            		 bodyHtml = CommonUtil.replace(bodyHtml, form.getHideTxtDefaultBody(), form.getHideHtmlDefaultBody());       		 
            	 }
            	 System.out.println("BODY=" + form.getBody());
            	 System.out.println("getHideTxtDefaultBody=" + form.getHideTxtDefaultBody());
            	 System.out.println("getHideHtmlDefaultBody=" + form.getHideHtmlDefaultBody());
            	 System.out.println("nEWBODY=" + (CommonUtil.isEmpty(bodyHtml) ? form.getBody() : bodyHtml));
            	 System.out.println("bodyHtml=" + bodyHtml);
                 params2.put(ProjectConstants.PARAM_SEND_EMAIL_WITH_BODY, CommonUtil.isEmpty(bodyHtml) ? form.getBody() : bodyHtml + "\n\n");
             }
             form.resetForm();
             session.setAttribute(sessionValueName, STATUS_SEND_EMAIL);
             session.setAttribute(ProjectConstants.SESSION_PARAMA_SEND_EMAIL, params2);
             handled=true;
         } else if(!handled && action.equalsIgnoreCase("selectTemplate")){
             String languageId = (String) session.getAttribute(ProjectConstants.SESSION_LANGUAGE_ID);
             SelectDelegate dg= new SelectDelegate();
             String quryStr="select Z08MSGSBJ,Z08ALTMSG	from z08 where Z08ALTMSGID='"+form.getMailTemplate()+"'"+
                             " AND Z08LCA='"+languageId+"'";
             ArrayList retList=(ArrayList) dg.selectList(quryStr);
            if(retList.size()!=0){
                ArrayList rslList=(ArrayList)retList.get(0);
                form.setSubject((String)rslList.get(0));
                form.setBody((String)rslList.get(1)+"\n\n"+form.getHidsubject());
            }


         }

         return actionForward;
    }
    
    public static String openPopWindowForEmail(String dotDo) {
        String str = "";
        str = "win  = window.open('" + dotDo + "'"+ ");";

        return str;
    }



}
